-- ============================================
-- LOGIN TERMINAL - saisie au clavier physique
-- Equivalent de login.lua, mais pour un Advanced Computer
-- (pas de clavier virtuel : on tape directement).
--
-- Utilisation : local login = dofile("login_term.lua")
--               local pseudo = login.authenticate()
--               -> retourne le pseudo, ou nil si le joueur annule
--
-- L'API est volontairement identique a login.lua pour que les
-- deux systemes coexistent sans se marcher dessus.
-- ============================================

local bank = dofile("client_solde.lua")

local TITLE = "ROCKET SLOTS"

local function fill(y, bg)
  local w = term.getSize()
  term.setBackgroundColor(bg)
  term.setCursorPos(1, y)
  term.write(string.rep(" ", w))
end

local function center(y, text, fg, bg)
  local w = term.getSize()
  term.setBackgroundColor(bg or colors.black)
  term.setTextColor(fg or colors.white)
  term.setCursorPos(math.floor((w - #text) / 2) + 1, y)
  term.write(text)
end

local function frame(sub)
  local w, h = term.getSize()
  term.setBackgroundColor(colors.black)
  term.clear()
  fill(1, colors.red)
  center(1, TITLE, colors.yellow, colors.red)
  fill(h, colors.gray)
  center(h, "Laisse vide et Entree pour annuler", colors.lightGray, colors.gray)
  if sub then center(3, sub, colors.lightGray, colors.black) end
end

-- lecture d'une ligne au clavier ; replace=nil pour du texte visible,
-- "*" pour masquer un mot de passe
local function prompt(y, label, replace)
  local w = term.getSize()
  term.setBackgroundColor(colors.black)
  term.setTextColor(colors.white)
  term.setCursorPos(3, y)
  term.clearLine()
  term.write(label)

  local bx = 3
  term.setCursorPos(bx, y + 1)
  term.setBackgroundColor(colors.gray)
  term.write(string.rep(" ", w - 4))
  term.setCursorPos(bx + 1, y + 1)
  term.setTextColor(colors.white)
  local value = read(replace)
  term.setBackgroundColor(colors.black)
  return value
end

-- petit ecran de bienvenue aux couleurs de la Team Rocket
local R_MINI = {
  "eeee..",
  "ee..ee",
  "eeee..",
  "ee.ee.",
  "ee..ee",
}

local function welcome(player, balance)
  local w, h = term.getSize()
  term.setBackgroundColor(colors.black)
  term.clear()
  local x0 = math.floor((w - 6) / 2) + 1
  local y0 = math.max(2, math.floor(h / 2) - 4)
  for i, row in ipairs(R_MINI) do
    term.setCursorPos(x0, y0 + i - 1)
    term.blit(string.rep(" ", 6), string.rep("f", 6), (row:gsub("%.", "f")))
  end
  center(y0 + #R_MINI + 1, "Bienvenue, " .. player .. " !", colors.yellow)
  center(y0 + #R_MINI + 2, "Credit : " .. tostring(balance), colors.lime)
  sleep(1.4)
end

-- Retourne le pseudo authentifie, ou nil si annulation
local function authenticate(promptText)
  while true do
    frame(promptText or "Connexion au reseau Team Rocket")
    local user = prompt(6, "Pseudo Minecraft :")
    if user == nil or user == "" then return nil end
    user = user:gsub("^%s+", ""):gsub("%s+$", "")
    if user == "" then return nil end

    local tries = 0
    while true do
      frame("Joueur : " .. user)
      local pass = prompt(6, "Mot de passe (vide = changer de pseudo) :", "*")
      if pass == nil or pass == "" then break end

      local ok, err = bank.authenticate(user, pass)
      if ok then
        local balance = bank.getBalance(user)
        welcome(user, balance)
        return user
      end

      tries = tries + 1
      center(10, err or "Erreur de connexion", colors.red)
      center(12, "Essai " .. tries .. " - Entree pour reessayer", colors.lightGray)
      sleep(1.4)
    end
  end
end

local function ask(promptText)
  frame(promptText)
  local v = prompt(6, "Pseudo Minecraft :")
  if v == "" then return nil end
  return v
end

return {
  ask = ask,
  authenticate = authenticate,
}
